/*
 * Copyright (c) 2023 iQi.Co.Ltd. All rights reserved.
 */
 
using IQIGame.Onigao.Framework;
using System.Collections.Generic;
using System.Linq;
using System;
using NetProtocol.Enum;

namespace NetProtocol.POD {

	/// <summary>
	///  房间
	/// module : room
	/// </summary>
	public class RoomPOD : IPOD 
    {
        public readonly static Func<RoomPOD> FACTORY = () => new RoomPOD();
		public static Func<RoomPOD> DynamicFactory = FACTORY;
        
		/*-- members --*/
		/// <summary>
        ///  房间id
        /// </summary>
		public long roomId;
		/// <summary>
        ///  座位信息
        /// </summary>
        private List<RoomSeatPod> m_seatInfos;
		/// <summary>
        ///  房主id
        /// </summary>
		public long leaderId;
		/// <summary>
        ///  关卡id - 可能0
        /// </summary>
		public int levelCid;
		
		
        public List<RoomSeatPod> seatInfos
        {
            get
            {
                if(m_seatInfos == null)
                {
                    m_seatInfos = new();
                }
                return m_seatInfos;
            }
            private set => m_seatInfos = value;
        }
        public List<RoomSeatPod> unsafe_seatInfos
        {
        	get => m_seatInfos;
        	set => m_seatInfos = value;
        }
		
		/*-- constructors --*/
		public RoomPOD() {}
		public RoomPOD(long roomId, List<RoomSeatPod> seatInfos, long leaderId, int levelCid) {
			this.roomId = roomId;
			this.m_seatInfos = seatInfos;
			this.leaderId = leaderId;
			this.levelCid = levelCid;
		}
		  public void Serial(SOutputStream _out) {
                if(roomId != default) {
                    _out.WriteByte(1);
                    _out.Write_long(roomId);
                }
                if(m_seatInfos != default) {
                    _out.WriteByte(2);
                    _out.Write_list_pod(m_seatInfos);
                }
                if(leaderId != default) {
                    _out.WriteByte(3);
                    _out.Write_long(leaderId);
                }
                if(levelCid != default) {
                    _out.WriteByte(4);
                    _out.Write_int(levelCid);
                }
        }
            
        public void UnSerial(SInputStream _in) {
            while (_in.HasRemaining())
            {
                int _key = _in.ReadByte();
                switch (_key)
                {
                    case 1:
                        roomId = _in.Read_long();
                        break;
                    case 2:
                        m_seatInfos = _in.Read_list_pod<RoomSeatPod>(RoomSeatPod.DynamicFactory);
                        break;
                    case 3:
                        leaderId = _in.Read_long();
                        break;
                    case 4:
                        levelCid = _in.Read_int();
                        break;
                    default: _in.SkipBinary();break;
                }
            }
        }
		
		public RoomPOD Clone() {
            RoomPOD o = DynamicFactory();
            o.roomId = roomId;
            if (m_seatInfos != null) {
                o.m_seatInfos = m_seatInfos.ConvertAll(o => o?.Clone());
            }
            o.leaderId = leaderId;
            o.levelCid = levelCid;
            return o;
        }
        
        public void Clear() {
            roomId = default;
            m_seatInfos = default;
            leaderId = default;
            levelCid = default;
        }
		
        public override string ToString() {
			string str = "";
			str += "RoomPOD [";
			str += "roomId=";
			str += roomId;
			str += ", seatInfos=";
			str += m_seatInfos.ToString<RoomSeatPod>();
			str += ", leaderId=";
			str += leaderId;
			str += ", levelCid=";
			str += levelCid;
			str += "]";
			return str;
		}
		
	}
}